﻿using System.Collections.Specialized;

namespace Phoenix.Xna.Framework.Collections
{
    public class UpdateableCollection<T> : GenericCollection<T>, IUpdateableCollection
    {
        #region GenericCollection<T> Members

        protected override void NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (!IsUpdating)
            {
                base.NotifyCollectionChanged(e);
            }
        }

        protected override void NotifyPropertyChanged(string property)
        {
            if (!IsUpdating)
            {
                base.NotifyPropertyChanged(property);
            }
        }

        protected virtual void NotifyUpdateComplete()
        {
            if (!IsUpdating)
            {
                base.NotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }

        #endregion

        #region IUpdateableCollection Members

        public bool IsUpdating { get; protected set; }

        public void BeginUpdate()
        {
            IsUpdating = true;
        }

        public void EndUpdate()
        {
            IsUpdating = false;
            NotifyUpdateComplete();
        }

        #endregion
    }
}
